<?php
require_once 'Zend/Controller/Action.php';

class EmailController extends Zend_Controller_Action 
{
    protected $_mail = null;

    public function init() {
        parent::init();
        Zend_Loader::loadClass('Zend_Mail');
        Zend_Loader::loadClass('Zend_Mail_Transport_Smtp');
        $options = array('ssl' => 'tls', 'port' => 25);
        $transport = new Zend_Mail_Transport_Smtp('192.168.0.100');
        Zend_Mail::setDefaultTransport($transport);
        
        $this->_mail = new Zend_Mail();
    }
    
    public function indexAction() {
        $this->render();
    }

    public function editAction() {
        $this->render();
    }

    public function sendAction() {
        $redirector = $this->getHelper('Redirector');
        $baseUrl = $this->view->baseUrl;

        if ($this->getRequest()->isPost()) {
            $params = $this->getRequest()->getParams();

            Zend_Loader::loadClass('Zend_Filter_StripTags');
            Zend_Loader::loadClass('Zend_Filter');
            $filterFormat = new Zend_Filter;
            $filterFormat->addFilter(new Zend_Filter_StripTags);

            // $this->filter...
            $email_from = $filterFormat->filter($params['email_from']);
            $email_to = $params['email_to'];
            $email_cc = $params['email_cc'];
            $email_bcc = $params['email_bcc'];
            $email_subject = $filterFormat->filter($params['email_subject']);
            $email_body = $filterFormat->filter($params['email_body']);
            $token = $params['token'];

            Zend_Loader::loadClass('Zend_Validate_EmailAddress');
            $validator = new Zend_Validate_EmailAddress();

            $mail = $this->_mail;

            

            if ($validator->isValid($email_to)) {
                $mail->addTo($email_to, $email_to);
                // for *nix system
                /*
                Zend_Loader::loadClass('Zend_Mail_Transport_Sendmail');
                $transportWithHeader = new Zend_Mail_Transport_Sendmail('-f'.$email_to);
                Zend_Mail::setDefaultTransport($transportWithHeader);
                $mail = new Zend_Mail();
                */
            }
            
            if ($validator->isValid($email_cc)) {
                $mail->addCc($email_cc, $email_cc);
            }

            if ($validator->isValid($email_bcc)) {
                $mail->addBcc($email_bcc, $email_bcc);
            }
            
            $mail->setFrom($email_from, 'Support');
            $mail->setSubject($email_subject);
            $mail->setBodyText($email_body);
        
            $error = false;
            
            /*
            Zend_Loader::loadClass('Zend_File_Transfer_Adapter_Http');
            $fileTransfer = new Zend_File_Transfer_Adapter_Http();
            $fileTransfer->setDestination(Zend_Registry::get('pathUploads'));
            $fileTransfer->addValidator('Size', 20000);
            if ($fileTransfer->isValid() and $fileTransfer->receive() !== false) {
            }
            */

            if (is_uploaded_file($_FILES['email_attachment']['tmp_name'])) {
                $fileName = Zend_Registry::get('pathUploads') . $_FILES['email_attachment']['name'];
                if (move_uploaded_file($_FILES['email_attachment']['tmp_name'], $fileName)) {
                    Zend_Loader::loadClass('Zend_Mime_Part');
                    Zend_Loader::loadClass('Zend_Mime');
                    $at = new Zend_Mime_Part(file_get_contents($fileName));
                    $at->filename = $_FILES['email_attachment']['name'];
                    $at->type = $_FILES['email_attachment']['type'];
                    $at->disposition = Zend_Mime::DISPOSITION_INLINE;
                    $at->encoding = Zend_Mime::ENCODING_BASE64;
                    $mail->addAttachment($at);
                } else {
                    $error = true;
                }
            }

            if ($error) {
                $flashMessenger = $this->getHelper('FlashMessenger');
                $flashMessenger->setNamespace('email')->addMessage('email error !');
                $redirector->gotoUrl($baseUrl . 'email/edit');
            } else {
                $mail->send();
            }
        }

        $redirector->gotoUrl($baseUrl);
    }

    public function listAction() {

    }
}